package com.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.dao.BaseDAO;
import com.entity.Baoxiu;
import com.entity.Notice;
import com.entity.Room;
import com.entity.Stu;
import com.entity.Userinfo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class NoticeAction extends ActionSupport {
	
	BaseDAO dao = new BaseDAO();
	Notice data;
	public String add() {
		data.setCurtime(new Date());
		dao.add(data);
		return show();
	}

	public String del() {
		dao.del(dao.findByID(Notice.class, data.getId()));
		return show();
	}

	public String show() {
		Userinfo role = (Userinfo) ServletActionContext.getRequest().getSession()
		.getAttribute("loginUser");
		String roomno="";
		if("学生".equals(role.getUserType())){
			String stuno = role.getLoginName();
			List<Stu> list = dao.find("from Stu where stuno=?", new Object[]{stuno});
			if(list!=null && list.size()>0){
				roomno = list.get(0).getRoomno();
				ActionContext.getContext().put("all", dao.find("from Notice where roomno=?", new Object[]{roomno}));
				return SUCCESS;
			}else{
				ActionContext.getContext().put("all", dao.find("from Notice where roomno=?", new Object[]{roomno}));
				return SUCCESS;
			}
			
			
		}else{
		ActionContext.getContext().put("all", dao.show("from Notice"));
		return SUCCESS;
		}
	}
	
	
	public String find() {
		String hql = "from Notice where 1=1";
		List params = new ArrayList();
		if(!"".equals(data.getArea())){
			hql+=" and area=?";
			params.add(data.getArea());
		}
		if(!"".equals(data.getFlat())){
			hql+=" and flat=?";
			params.add(data.getFlat());
		}
		if(!"".equals(data.getRoomno())){
			hql+=" and roomno=?";
			params.add(data.getRoomno());
		}
		ActionContext.getContext().put("all", dao.find(hql, params.toArray()));
		return "notice";
	}
	

	public Notice getData() {
		return data;
	}

	public void setData(Notice data) {
		this.data = data;
	}
}
